//
//  HWGameTestAlert.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/11/10.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWGameTestAlert: MMPopupView {
    private var tapBgView:UIView!
    private var bgView: UIView!
    var nameLabel:UILabel!
    var contentLabel:UILabel!
    var cancelButton:UIButton!
    var model:HWGameTestModel? {
        didSet {
            nameLabel.text = model?.name
            contentLabel.text = model?.content
        }
    }
    @objc var close:(()->())?
    var copyButtonClick:((_:String)->())?
    var receiveButtonClick:((_:String)->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()

    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func show() {
        super.show()
    }
    
    override func hide() {
        super.hide()
    }

}

extension HWGameTestAlert {
    private func initUI() -> Void {
        type = MMPopupType.alert
        backgroundColor = UIColor.clear
        attachedView = UIApplication.shared.windows.filter({ $0.isKeyWindow }).last?.rootViewController?.view
        
        tapBgView = UIView.init()
        tapBgView.backgroundColor = UIColor.clear
        addSubview(tapBgView)
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(bacgroundTap(_:)))
        tapBgView.addGestureRecognizer(tap)
        
        bgView = UIView()
        bgView.backgroundColor = .white
        bgView.layer.cornerRadius = 18*widthScale
        addSubview(bgView)
        
        cancelButton = UIButton(type: .custom)
        cancelButton.setTitle("关闭", for: .normal)
        cancelButton.addTarget(self, action: #selector(cancelButtonClick(_:)), for: .touchUpInside)
        cancelButton.setTitleColor(.colorWithHexString(hexString: "#888888"), for: .normal)
        cancelButton.titleLabel?.font = .boldSystemFont(ofSize: 13*widthScale)
        cancelButton.layer.cornerRadius = 14*widthScale
        bgView.addSubview(cancelButton)

        nameLabel = HWLabel.createBoldLabel(title: "", color: "#333333", font: 14*widthScale)
        nameLabel.textAlignment = .center
        bgView.addSubview(nameLabel)
        
        contentLabel = HWLabel.createLabel(title: "", color: "#888888", font: 11*widthScale)
        bgView.addSubview(contentLabel)
        
        
    }
    
    private func initFrame() -> Void {
        snp.makeConstraints { make in
            make.width.equalTo(screenWidth)
            make.height.equalTo(screenHeight)
        }
        
        tapBgView.snp.makeConstraints { make in
            make.top.left.right.bottom.equalTo(0)
        }
        
        bgView.snp.makeConstraints { make in
            make.left.equalTo(15*widthScale)
            make.right.equalTo(-15*widthScale)
            make.center.equalToSuperview()
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(15*widthScale)
            make.right.equalTo(-15*widthScale)
            make.top.equalTo(15*widthScale)
        }
        
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(15*widthScale)
            make.right.equalTo(-15*widthScale)
            make.top.equalTo(nameLabel.snp.bottom).offset(15*widthScale)
        }
        
        cancelButton.snp.makeConstraints { make in
            make.top.equalTo(contentLabel.snp.bottom).offset(15*widthScale)
            make.bottom.equalTo(-15*widthScale)
            make.width.equalTo(100*widthScale)
            make.height.equalTo(40*widthScale)
            make.centerX.equalToSuperview()
        }
        
    }
}
extension HWGameTestAlert {
    @objc func bacgroundTap(_ tap:UITapGestureRecognizer) -> Void {
        hide()
    }
    
    @objc func cancelButtonClick(_ button:UIButton) -> Void {
        hide()
    }
    
}

